import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitCase {
    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("学习之路","zhangsan","http://42.193.100.184:8080/java/blog_list.html"));

    }
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "tat.csv")
    public void loginTest(String username,String password,String url){
        webDriver.get("http://42.193.100.184:8080/java/login.html");
       webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
       webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
       webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
       webDriver.findElement(By.id("submit")).click();
       String currentUrl = webDriver.getCurrentUrl();
      Assertions.assertEquals(url,currentUrl);
      System.out.println("1");  //代表测试通过
    }



     //这个是用来设计登录失败的场景

    /*
    @ParameterizedTest
    @CsvFileSource(resources = "tbt.csv")
    public void loginTest1(String username,String password,String url){
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("http://42.193.100.184:8080/java/login.html");
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.id("submit")).click();
        String currentUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals(url,currentUrl);
        System.out.println("1");  //代表测试通过,否则说明登录失败
    }

     */

    //对博客列表也进行测试
    @Order(2)
    @ParameterizedTest
    @CsvFileSource(resources = "tct.csv")
    public void blogListTest(String username,String url){
     String actualName =   webDriver.findElement(By.xpath("/html/body/div[2]/div[1]/div/h3")).getText();
       Assertions.assertEquals(username,actualName);
       List<WebElement> webElements =  webDriver.findElements(By.className("title"));
       int num = webElements.size();
        System.out.println(num);
       Assertions.assertNotEquals(0,num);
       webDriver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > a")).click();
       webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String currentUrl = webDriver.getCurrentUrl();
       if(currentUrl.contains(url)){
           System.out.println("测试通过");
           return;
       }
        System.out.println("测试不通过");

    }

    //对博客详情页进行测试

    @Order(3)
    @ParameterizedTest
   @MethodSource("Generator")
    public void blogDetailTest(String title,String name,String url) throws InterruptedException {
       String title1 =  webDriver.findElement(By.cssSelector("body > div.container > div.container-right > h3")).getText();
        Assertions.assertEquals(title,title1);
        String name1 = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        Assertions.assertEquals(name,name1);
        Thread.sleep(3000);
        webDriver.findElement(By.linkText("主页")).click();
        String currentUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals(url,currentUrl);
    }

    //对写博客进行测试设计
    @Order(4)
    @ParameterizedTest
    @CsvFileSource(resources = "tet.csv")
    public void writeBlogTest(String biaoTi,String url) throws InterruptedException {
        webDriver.findElement(By.linkText("写博客")).click();
        webDriver.findElement(By.cssSelector("#title-input")).sendKeys(biaoTi);
        Thread.sleep(3000);
        webDriver.findElement(By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap > div.CodeMirror-scroll")).sendKeys("在学习道路中一定要稳住自己的心态克服困难才会迎来曙光");
        Thread.sleep(3000);
        webDriver.findElement(By.cssSelector("#submit")).click();
        String currentUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals(url,currentUrl);
    }
    @Order(5)
    @ParameterizedTest
    @CsvFileSource(resources = "tft.csv")
    public void zhuXiaoTest(String url){
        webDriver.findElement(By.linkText("注销")).click();
        String currentUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals(url,currentUrl);
    }

}
